﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Web;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using SDSystem.Models;

namespace SDSystem.Controllers
{
    public class SiteFriendController : Controller
    {
        private ForumEntities db=new ForumEntities();
        // GET: /SiteFriend/

        public ActionResult Index()
        {
            return View();
        }

        //Create Site Friend
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(SiteFriend sf)
        {
            if(ModelState.IsValid)
            {
                sf.IsActive = true;
                db.SiteFriends.AddObject(sf);
                db.SaveChanges();
                X.Msg.Notify("Successfull", "SiteFriend is saved").Show();
                return this.FormPanel();
            }
            X.Msg.Notify("Failed", "No SiteFriend is created");
            return this.FormPanel();
        }

        //Site Friend Data
        public ActionResult SiteFriendData(StoreRequestParameters parameters)
        {
            var result = from siteDB in db.SiteFriends
                         select new
                                    {
                                        siteDB.SiteFriendId,
                                        siteDB.SiteName,
                                        siteDB.Url,
                                        siteDB.IsActive
                                    };

            // Sorting
            if (!string.IsNullOrEmpty(parameters.SimpleSort))
            {
                result =
                    result.OrderBy(parameters.SimpleSort +
                                   (parameters.SimpleSortDirection == SortDirection.DESC ? " desc" : " asc"));
            }
            else
            {
                result = result.OrderBy(ent => ent.SiteFriendId);
            }

            // Paging
            int limit = parameters.Limit;
            if ((parameters.Start + parameters.Limit) > result.Count())
            {
                limit = result.Count() - parameters.Start;
            }

            var rangeData = (parameters.Start < 0 || limit < 0) ? result : result.Skip(parameters.Start).Take(limit);
            return this.Store(rangeData, result.Count());

        }

    }
}
